//index.js
//获取应用实例
var app = getApp()

Page({
  data: {
    swiperImg: [ 
      {
        linkUrl: 'details/index',
        imgUrl:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/pd/cf-4.jpg'
      },
      {
        linkUrl: 'details/index',
        imgUrl:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/pd/cf-5.jpg'
      },
      {
        linkUrl: 'details/index',
        imgUrl:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/pd/cf-8.jpg'
      }
    ],
      indicatorDots: true,//面板指示点
      autoplay: true,//自动切换
      interval: 3000,//自动切换时间间隔
      duration: 500, //滑动动画时长
    //菜单列表
    navment:[
      { url:'index', icon:'shuiguo', navName:'新鲜水果', color:'1' },
      { url:'index', icon:'haixianshuichan', navName:'海鲜水产' , color:'2'},
      { url:'index', icon:'shengxian-shucai', navName:'时令鲜蔬', color:'3' },
      { url:'index', icon:'rouqin', navName:'精选肉禽', color:'4' },
      { url:'index', icon:'milk', navName:'牛奶乳品' , color:'5'},
      { url:'index', icon:'liangyou', navName:'粮油副食', color:'6' },
      { url:'index', icon:'lingshi', navName:'休闲零食', color:'7'},
      { url:'index', icon:'jiushui', navName:'酒水饮料', color:'8' }
    ],
    //广告位
    adsenseList:[
      {id:'01', urlLink:'details/index', urlImg:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/ad/tou-6.jpg'},
      {id:'02', urlLink:'details/index', urlImg:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/ad/tou-5.jpg'},
      {id:'03', urlLink:'details/index', urlImg:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/ad/tou-4.jpg'},
      {id:'04', urlLink:'details/index', urlImg:'http://demo.demohuo.top/modals/62/6201/demo/themes/img/ad/tou-3.jpg'}
    ],
    
    //横向滚动列表
     titleBlock:'满减热销',
     scrollList:[
      {
        id:'01', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',  
        urlImg:'http://demo.demohuo.top/modals/64/6496/demo/images/26.png'
      },
      {
        id:'02', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',   
        urlImg:'http://demo.demohuo.top/modals/64/6496/demo/images/28.png'
      },
      {
        id:'03', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',  
        urlImg:'http://demo.demohuo.top/modals/64/6496/demo/images/25.png'
      },
      {
        id:'04', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',  
        urlImg:'http://demo.demohuo.top/modals/64/6496/demo/images/27.png'
      },
      {
        id:'05', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',  
        urlImg:'http://demo.demohuo.top/modals/64/6496/demo/images/22.png'
      },
      {
        id:'06', 
        scrollUrl:'details/index',
        waresName:'休闲迷彩磨砂真皮运动鞋女单鞋', 
        waresPrice:'￥286.0',  
        urlImg:'https://img.alicdn.com/imgextra/i3/17971277/O1CN01YQ7bKy1LIuHHlZ4pp_!!0-saturn_solar.jpg_220x220.jpg'
      }
     ],

     //单个广告位
     adsenseImg:'http://demo.demohuo.top/modals/64/6496/demo/images/ad.jpg',

     //商品列表
     titleLike:'猜您喜欢',
     pictureList:[
      {
        id:'07', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂运动鞋女单鞋', 
        price:'￥286.0',  
        urlImg:'http://img.alicdn.com/imgextra/i2/45854176/O1CN01zO7Ovh1gieZ5Jcroh_!!0-saturn_solar.jpg_220x220.jpg'
      },
      {
        id:'08', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂真皮运动鞋女单鞋', 
        price:'￥286.0',   
        urlImg:'http://img.alicdn.com/imgextra/i2/34261942/O1CN01olN1iE1QDTkFBafMj_!!0-saturn_solar.jpg_220x220.jpg'
      },
      {
        id:'09', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂真皮运动鞋女单鞋', 
        price:'￥286.0',  
        urlImg:'http://img.alicdn.com/imgextra/i2/110102149/O1CN014MrTdS1RkHjjrsVq2_!!0-saturn_solar.jpg_220x220.jpg'
      },
      {
        id:'10', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂真皮运动鞋女单鞋', 
        price:'￥286.0',  
        urlImg:'http://img.alicdn.com/imgextra/i1/115237817/O1CN01qO2Xvy27cEbVYAYzM_!!0-saturn_solar.jpg_220x220.jpg'
      },
      {
        id:'11', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂真皮运彩磨砂真皮运动鞋女单鞋', 
        price:'￥286.0',  
        urlImg:'http://img.alicdn.com/imgextra/i1/793810113/O1CN01Gw4J9E1ChnKnTxonI_!!0-saturn_solar.jpg_220x220.jpg'
      },
      {
        id:'12', 
        pictureUrl:'details/index',
        picName:'韩国代购正品超爆款 休闲迷彩磨砂真皮运动鞋女单鞋', 
        price:'￥286.0',  
        urlImg:'http://img.alicdn.com/imgextra/i4/130024419/O1CN01o0hzFU1iVwns9jD0m_!!0-saturn_solar.jpg_220x220.jpg'
      }
     ],
  },
  //scroll-view
  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },
    //获取当前位置
    onChangeAddress: function() {
       var _page = this;
       wx.chooseLocation({
         success: function(res) {
         _page.setData({
            chooseAddress: res.name
          });
       },
        fail: function(err) {
          console.log(err)
        }
      });
    },
    
    // 加入购物车
    addcart:function(e){
      this.setData({
          toastHidden:false
      });
      // 遍历列表 与 购物车列表
      for (var i in this.data.pictureList){
          // 列表中某一项item的id == 点击事件传递过来的id。则是被点击的项
          if(this.data.pictureList[i].id == e.target.id){
              // 给pictureList数组的当前项添加count元素，值为1，用于记录添加到购物车的数量
              this.data.pictureList[i].count = 1;
              // 获取购物车的缓存数组（没有数据，则赋予一个空数组）
              var arr = wx.getStorageSync('cart') || [];
              // 如果购物车有数据
              if(arr.length>0){
                  // 遍历购物车数组
                  for(var j in arr){
                      // 判断购物车内的item的id，和事件传递过来的id，是否相等
                      if(arr[j].id == e.target.id){
                          // 相等的话，给count+1（即再次添加入购物车，数量+1）
                          arr[j].count = arr[j].count + 1;
                          // 最后，把购物车数据，存放入缓存（此处不用再给购物车数组push元素进去，因为这个是购物车有的，直接更新当前数组即可）
                          try {
                              wx.setStorageSync('cart', arr)
                          } catch (e) {
                              console.log(e)
                          }
                          // 返回（在if内使用return，跳出循环节约运算，节约性能）
                          return;
                      }
                  }
                  // 遍历完购物车后，没有对应的item项，把pictureList的当前项放入购物车数组
                  arr.push(this.data.pictureList[i]);
              }
              // 购物车没有数据，把item项push放入当前数据（第一次存放时）
              else{
                  arr.push(this.data.pictureList[i]);
              }
              // 最后，把购物车数据，存放入缓存
              try {
                  wx.setStorageSync('cart', arr)
                  // 返回（在if内使用return，跳出循环节约运算，节约性能）
                  return;
              } catch (e) {
                  console.log(e)
              }
          }
      }
  }

})
